package com.rogue.blog.controller.article;

import com.rogue.blog.model.dto.article.ArticleDTO;
import com.rogue.blog.model.po.PageBean;
import com.rogue.blog.model.po.PageCondition;
import com.rogue.blog.model.vo.ResponseVO;
import com.rogue.blog.model.vo.article.ArticleCardVO;
import com.rogue.blog.model.vo.article.ArticleVO;
import com.rogue.blog.service.article.ArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * Created by 86101 on 2018-10-22.
 */
@Api(tags = {"文章"})
@RestController
@RequestMapping(value = "")
public class ArticleController {

    private final static Logger logger = LoggerFactory.getLogger(ArticleController.class);

    @Autowired
    private ArticleService articleService;

    @ApiOperation(value = "添加文章")
    @RequestMapping(value = "/article", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseVO<Long> insert(@RequestBody ArticleDTO dto) throws Exception {
        long articleId = articleService.insert(dto);
        ResponseVO<Long> responseVO = new ResponseVO<>();
        if (StringUtils.isEmpty(articleId)) {
            responseVO.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
            responseVO.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.name());
            responseVO.setMessage("Insert article error.");
        } else {
            responseVO.setCode(HttpStatus.OK.value());
            responseVO.setStatus(HttpStatus.OK.name());
            responseVO.setMessage("Insert article success.");
            responseVO.setData(articleId);
        }
        return responseVO;
    }

    @ApiOperation(value = "根据ID删除文章")
    @RequestMapping(value = "/article/{articleId}", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseVO<Integer> deleteById(@PathVariable long articleId) throws Exception {
        ResponseVO<Integer> responseVO = new ResponseVO<>();
        int row = articleService.deleteById(articleId, false);
        if (0 == row) {
            responseVO.setCode(HttpStatus.NOT_FOUND.value());
            responseVO.setStatus(HttpStatus.NOT_FOUND.name());
        } else {
            responseVO.setCode(HttpStatus.OK.value());
            responseVO.setStatus(HttpStatus.OK.name());
        }
        responseVO.setMessage("Affected rows " + row + ".");
        responseVO.setData(row);
        return responseVO;
    }

    @ApiOperation(value = "根据ID更新文章")
    @RequestMapping(value = "/article/{articleId}", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseVO<Integer> updateById(@RequestBody ArticleDTO dto, @PathVariable long articleId) throws Exception {
        ResponseVO<Integer> responseVO = new ResponseVO<>();
        ArticleVO articleVO = articleService.selectById(articleId);
        if (null == articleVO) {
            responseVO.setCode(HttpStatus.NOT_FOUND.value());
            responseVO.setStatus(HttpStatus.NOT_FOUND.name());
            responseVO.setMessage("Not found article by the id " + articleId + ". Please check the parameters.");
        } else {
            int row = articleService.updateById(articleId, dto);
            responseVO.setCode(HttpStatus.OK.value());
            responseVO.setStatus(HttpStatus.OK.name());
            responseVO.setMessage("Affected rows " + row + ".");
            responseVO.setData(row);
        }
        return responseVO;
    }

    @ApiOperation(value = "根据ID获取文章")
    @RequestMapping(value = "/article/{articleId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseVO<ArticleVO> selectById(@PathVariable long articleId) throws Exception {
        ResponseVO<ArticleVO> responseVO = new ResponseVO<>();
        ArticleVO articleVO = articleService.selectById(articleId);
        if (null == articleVO) {
            responseVO.setCode(HttpStatus.NOT_FOUND.value());
            responseVO.setStatus(HttpStatus.NOT_FOUND.name());
            responseVO.setMessage("Not found article by the id " + articleId + ". Please check the parameters.");
        } else {
            responseVO.setCode(HttpStatus.OK.value());
            responseVO.setStatus(HttpStatus.OK.name());
            responseVO.setMessage("Found article by the id " + articleId + ".");
            responseVO.setData(articleVO);
        }
        return responseVO;
    }

    @ApiOperation(value = "分页获取文章")
    @RequestMapping(value = "/articles", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseVO<PageBean<ArticleCardVO>> selectByPage(@RequestParam int currentPage,
                                                            @RequestParam int pageSize,
                                                            @RequestParam(required = false) String search) throws Exception {
        ResponseVO<PageBean<ArticleCardVO>> responseVO = new ResponseVO<>();
        PageBean<ArticleCardVO> voPageBean = articleService.selectByPage(currentPage, pageSize, search);
        if (CollectionUtils.isEmpty(voPageBean.getPageData())) {
            responseVO.setCode(HttpStatus.NOT_FOUND.value());
            responseVO.setStatus(HttpStatus.NOT_FOUND.name());
            responseVO.setMessage("Not found articles. Please check the parameters.");
            responseVO.setData(voPageBean);
        } else {
            responseVO.setCode(HttpStatus.OK.value());
            responseVO.setStatus(HttpStatus.OK.name());
            responseVO.setMessage("Found articles.");
            responseVO.setData(voPageBean);
        }
        return responseVO;
    }

    @ApiOperation(value = "文章是否存在")
    @RequestMapping(value = "/article/exists", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseVO<Boolean> exists(@RequestParam String articleName, @RequestParam long articleId) throws Exception {
        ResponseVO<Boolean> responseVO = new ResponseVO<>();
        boolean exists = articleService.exists(articleName, articleId);
        if (exists) {
            responseVO.setCode(HttpStatus.OK.value());
            responseVO.setStatus(HttpStatus.OK.name());
            responseVO.setMessage("Found article by the articleName " + articleName + ".");
            responseVO.setData(exists);
        } else {
            responseVO.setCode(HttpStatus.NOT_FOUND.value());
            responseVO.setStatus(HttpStatus.NOT_FOUND.name());
            responseVO.setMessage("Not found article by the articleName " + articleName + ".");
            responseVO.setData(exists);
        }
        return responseVO;
    }
}
